﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;

namespace BitPortal.Business
{
    public static partial class ProductPropertyBLL
    {
        public static string GenerateCategory(string[] categoryIds)
        {
            string str = "#";
            foreach (string categoryID in categoryIds)
            {
                str += categoryID + "#";
            }
            return str;
        }

        public static string[] ReturnCategory(string appliedCategory)
        {
            return appliedCategory.Split(new char[] { '#' });
        }

        public static int Insert(this Models.ProductProperty property)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            bitEnt.ProductProperties.AddObject(property);
            return bitEnt.SaveChanges();
        }

        public static int Delete(int propertyID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            ProductProperty property = bitEnt.ProductProperties.SingleOrDefault(ent => ent.PropertyID == propertyID);
            if (property != null)
            {
                bitEnt.ProductProperties.DeleteObject(property);
            }
            return bitEnt.SaveChanges();

        }

        public static IQueryable<ProductProperty> GetAll()
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            return bitEnt.ProductProperties;
        }

        public static IQueryable<ProductProperty> GetByCondition(string productCategoryID = null, bool? isEnabled = null, bool? applyToAllProduct = null,
            bool? allowAdvanceSearch = null, bool? allowFilter = null)
        {
            var list = GetAll();
            return list.Where(ent =>
                (productCategoryID == null || ent.AppliedCatgoryID.Contains("#" + productCategoryID + "#")) &&
                (isEnabled == null || ent.IsEnabled == isEnabled) &&
                (applyToAllProduct == null || ent.ApplyToAllProduct == applyToAllProduct) &&
                (allowAdvanceSearch == null || ent.AllowAdvanceSearch == allowAdvanceSearch) &&
                (allowFilter == null || ent.AllowFilter == allowFilter)
                );
        }
        
        public static ProductProperty GetByID(int propertyID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            return bitEnt.ProductProperties.SingleOrDefault(ent => ent.PropertyID == propertyID);
        }

        /// <summary>
        /// Lay ra danh sach thuoc tinh ung voi mot danh muc san pham
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public static List<ProductProperty> GetPropertiesForCategory(ProductCategory category = null)
        {
            List<ProductProperty> result = GetByCondition(applyToAllProduct: true).ToList() ;
            if (category != null)
            {
                result = result.Union(GetByCondition(productCategoryID: category.CategoryID.ToString(), applyToAllProduct: false).ToList(),new ProductPropertyComparer()).ToList();
                foreach (ProductCategory parent in category.GetParents())
                {
                    result = result.Union(GetByCondition(productCategoryID: parent.CategoryID.ToString(), applyToAllProduct: false).ToList(), new ProductPropertyComparer()).ToList();
                }
            }
            return result;
        }

        public class ProductPropertyComparer : IEqualityComparer<ProductProperty>
        {
            public bool Equals(ProductProperty x, ProductProperty y)
            {
                return x.PropertyID == y.PropertyID;
            }
            public int GetHashCode(ProductProperty obj)
            {
                return obj.PropertyID.GetHashCode();
            }
        }
    }
}